<script setup>
import { ref, onMounted } from 'vue'
import { getAllApplications } from '@/api/admin'
import { convertStatusToNumber } from '@/utils/enum'

const searchForm = ref({ page: 1, size: 10 })
const applications = ref([])
const total = ref(0)
const loading = ref(false)
const statusMap = {
  PENDING: '待处理',
  REJECTED: '已拒绝',
  INTERVIEW_INVITED: '已邀请面试'
}
const statusTagType = status => {
  const numStatus = convertStatusToNumber(status, 'application')
  if (numStatus === 0) return 'info'
  if (numStatus === 1) return 'danger'
  if (numStatus === 2) return 'success'
  return ''
}
const fetchApplications = async () => {
  loading.value = true
  try {
    const params = { ...searchForm.value }
    const res = await getAllApplications(params)
    const data = res.data || res
    applications.value = data.list || []
    total.value = data.total || 0
    if (data.pageNum) searchForm.value.page = data.pageNum
    if (data.pageSize) searchForm.value.size = data.pageSize
  } catch (e) {}
  loading.value = false
}
const resetSearch = () => {
  searchForm.value = { page: 1, size: 10 }
  fetchApplications()
}
const onPageChange = page => {
  searchForm.value.page = page
  fetchApplications()
}
onMounted(() => {
  fetchApplications()
})
</script>

<template>
  <el-card class="admin-application-list-card">
    <h2>全平台求职申请管理</h2>
<!--    <el-form :inline="true" :model="searchForm" class="search-form" @submit.prevent>-->
<!--      <el-form-item>-->
<!--        <el-button type="primary" @click="fetchApplications">筛选</el-button>-->
<!--        <el-button @click="resetSearch">重置</el-button>-->
<!--      </el-form-item>-->
<!--    </el-form>-->
    <el-table :data="applications" stripe border class="application-table" v-loading="loading">
      <el-table-column prop="position_name" label="岗位名称" min-width="120" />
      <el-table-column prop="enterprise_name" label="企业名称" min-width="120" />
      <el-table-column prop="studentName" label="学生姓名" min-width="100" />
      <el-table-column prop="apply_time" label="投递时间" min-width="140" />
      <el-table-column prop="status" label="状态" min-width="100">
        <template #default="{ row }">
          <el-tag :type="statusTagType(row.status)">{{ statusMap[row.status] }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" min-width="120" />
    </el-table>
    <div class="pagination-bar">
      <el-pagination
          background
          layout="prev, pager, next, jumper"
          :total="total"
          :page-size="searchForm.size"
          :current-page="searchForm.page"
          @current-change="onPageChange"
      />
    </div>
  </el-card>
</template>
